package com.ht.activiti.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.ht.activiti.constant.SystemConstant;
import com.ht.activiti.service.ExeclService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author zhangfc
 * @date 2021/10/29 8:19
 */
@Service
public class ExeclServiceImpl implements ExeclService {

    //保证当FLAG发生变化时 其他线程立即读取到FLAG的内容
    private volatile boolean FLAG = false;

    @Override
    public List<Map<String,Object>> execlTransFormFile(InputStream ins, String fileOriginName) throws Exception {

        //获取文件中所有数据
        List<List<Object>> ececlAllData = getEceclAllData(ins, fileOriginName);
        System.out.println(ececlAllData);
        //重新组装list
        List<Map<String, Object>> reComeBineList = new ArrayList<>();


        System.out.println("ssfdsaf");
        //读取单位
        String unit = null;
        //截取总体纸样资料  -----  纸样详细资料 之间的数据
        for (List<Object> ececlAllDatum : ececlAllData) {
            //获取工作单位
            if (StrUtil.containsAny(ececlAllDatum.get(0).toString(), "工作单位")) {
                unit = ececlAllDatum.get(1).toString();
            }
            //此行读取结束跳出循环
            if (StrUtil.equals(Convert.toStr(ececlAllDatum.get(0)),"纸样详细资料")) {
                FLAG = false;
                break;
            }
            if (FLAG) {
                Map<String, Object> map = new LinkedHashMap<>();
                for (int i = 0; i < ececlAllDatum.size(); i++) {
                    map.put(SystemConstant.ExeclConstant.ROW_LIST.get(i), ececlAllDatum.get(i));
                }
                reComeBineList.add(map);

            }
            //此行代表读取开始
            if (StrUtil.equals(Convert.toStr(ececlAllDatum.get(0)),"纸样名称")) {
                FLAG = true;
            }
        }

        List<Map<String, Object>> reponseList = new ArrayList<>();
        //对截取后的list进行处理
        for (Map<String, Object> map : reComeBineList) {
            //去除掉key为 面积 周界的值,将纸样说明的值X*y切割成两个值分别为长和宽,重新计算面积
            map.remove("面积");
            map.remove("周界");
            String zysm = Convert.toStr(map.get("纸样说明"));

            String[] split = zysm.split("\\*");
            //长
            BigDecimal lengthBig = Convert.toBigDecimal(split[0]);
            //宽
            BigDecimal widthBig = Convert.toBigDecimal(split[1]);

            //小数点后保留两位,整数位没有数据保留0
            DecimalFormat tamplet = new DecimalFormat("0.00");

            //面积(平方英寸) = 长 * 宽 / 6.4516
            String format = tamplet.format(lengthBig.multiply(widthBig).subtract(SystemConstant.ExeclConstant.CM2_INCH2).doubleValue());

            map.put("面积",format);
            map.put("长",lengthBig.doubleValue());
            map.put("宽",widthBig.doubleValue());

            map.remove("纸样说明");
            reponseList.add(map);
        }

        return reponseList;
    }

    /**
     * 获取execl中所有的数据内容
     *
     * @param ins            输入流
     * @param fileOriginName 文件名
     * @return List
     * @throws Exception 异常
     */
    private List<List<Object>> getEceclAllData(InputStream ins, String fileOriginName) throws Exception {
        List<List<Object>> list = new ArrayList();

        //创建Execl工作薄
        Workbook workbook = this.getWorkbook(ins, fileOriginName);
        if (workbook == null) {
            throw new Exception("创建Execl工作薄为空");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;

        //读取execl表中的行和列,并将内容存入List中
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            sheet = workbook.getSheetAt(i);
            if (sheet == null) {
                continue;
            }
            //读取每个sheet中的行内容
            for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if (row == null || row.getFirstCellNum() == j) {
                    continue;
                }
                List<Object> li = new ArrayList<>();

                //读取每行的列内容,并将每列的内容存入list中
                for (int k = row.getFirstCellNum(); k < row.getLastCellNum(); k++) {
                    cell = row.getCell(k);
                    li.add(cell);
                }
                list.add(li);
            }
        }
        workbook.close();
        return list;
    }

    /**
     * 创建Execl工作薄
     *
     * @param ins            输入流
     * @param fileOriginName 文件名
     * @throws Exception 异常
     */
    private Workbook getWorkbook(InputStream ins, String fileOriginName) throws Exception {

        Workbook workbook = null;

        String fileType = fileOriginName.substring(fileOriginName.lastIndexOf("."));
        if (".xls".equals(fileType)) {
            workbook = new HSSFWorkbook(ins);
        } else if (".xlsx".equals(fileType)) {
            workbook = new XSSFWorkbook(ins);
        } else {
            throw new Exception("请上传Execl格式文件");
        }
        return workbook;
    }
}
